package cn.edu.scut.sys.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.scut.sys.service.DepartmentService;
import cn.edu.scut.sys.utils.DataRequest;
import cn.edu.scut.sys.utils.DataResponse;
import cn.edu.scut.sys.utils.StringUtils;
import cn.edu.scut.sys.vo.Department;


@Controller
@RequestMapping("/department")
public class DepartmentController {

	private DepartmentService departmentService;

	@Resource(name="departmentService")
	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}
	
	/**
	 * 
	 * @param page
	 * @param rows
	 * @param sidx
	 * @param sord
	 * @param search
	 * @param searchField
	 * @param searchOper
	 * @param searchString
	 * @param filters
	 * @return
	 */
	@RequestMapping("/list")
	@ResponseBody
	public DataResponse<Department> list(@RequestParam(defaultValue="1",value="page") String page,  
            @RequestParam(defaultValue="20",value="rows") String rows,  
            @RequestParam("sidx") String sidx,  
            @RequestParam("sord") String sord,  
            @RequestParam("search") boolean search,  
            @RequestParam(required=false,value="searchField") String searchField,  
            @RequestParam(required=false,value="searchOper") String searchOper,  
            @RequestParam(required=false,value="searchString") String searchString,  
            @RequestParam(required=false,value="filters") String filters  
            ){  
		 try {  
	            DataRequest request = new DataRequest();  
	            request.setPage(StringUtils.isEmpty(page) ? 1 : Integer.valueOf(page));  
	            request.setRows(StringUtils.isEmpty(rows) ? 20 : Integer.valueOf(rows));  
	            request.setSidx(sidx);  
	            request.setSord(sord);  
	            request.setSearch(search);  
	            request.setSearchField(searchField);  
	            request.setSearchOper(searchOper);  
	            request.setSearchString(searchString);  
	            return departmentService.list(request);  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        }  
	        return null;  
	}
	
	@RequestMapping("/add")
	public String add(String name, String address,String principal,HttpServletRequest request){
		
		try{
			
			// 外单位增加
			Department department = new Department();
			department.setName(name);
			if (null!= address)
			department.setAddress(address);
			if (null!=principal)
			department.setPrincipal(principal);
			departmentService.add(department);
			request.setAttribute("status", 1);
		}catch(Exception ex){
			request.setAttribute("status", 0);
		}
			
			return "/page/departmentAdd";

	}
	
	
	@RequestMapping("/edit/{id}")
	public String edit(@PathVariable(value = "id") String id,HttpServletRequest request){
		if (null == id) {
			return "redirect:/page/departmentList.jsp";
		}
		
		Department department=departmentService.getDepartment(id);
		
		if (null == department) {
			return "redirect:/page/departmentList.jsp";
		}
		
		request.setAttribute ("department", department);
		
		return "page/departmentEdit";
	}
	
	@RequestMapping("/update")
	public String update(Department department,HttpServletRequest request){
		try{
		if (null == department) {
			System.out.println("department");
			request.setAttribute("status", 0);
			return "/page/departmentEdit";
		}

		if (null == department.getName() || department.getName().isEmpty()) {
			System.out.println("departmentname");
			request.setAttribute("status", 0);
			return "/page/departmentEdit";
		}
		departmentService.updateDepartment(department);
		request.setAttribute("status", 1);
		return "/page/departmentEdit";
		}
		catch(Exception e){
			request.setAttribute("status", 0);
			return "/page/departmentEdit";
		}
		
	}
	
	
	@RequestMapping("/del/{id}")
	public String del(@PathVariable(value = "id") int id,HttpServletRequest request){
		departmentService.delDepartment(id);
	return "redirect:/page/departmentList.jsp";
	}
}
